package com.qianxun.module.datasource1.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianxun.module.datasource1.admin.entity.SysUserFunctionRoleRelation;
import com.qianxun.module.datasource1.admin.mapper.SysUserFunctionRoleRelationMapper;
import com.qianxun.module.datasource1.admin.service.SysUserFunctionRoleRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 功能角色和用户的映射  服务实现类
 * </p>
 *
 * @author admin
 * @since 2019-11-28
 */
@Service
public class SysUserFunctionRoleRelationServiceImpl extends ServiceImpl<SysUserFunctionRoleRelationMapper, SysUserFunctionRoleRelation> implements SysUserFunctionRoleRelationService {
    @Override
    public void setAuthority(String userUuid, Integer[] fnRoleIds) {
        //删除角色
        this.baseMapper.deleteByUserUuid(userUuid);

        List<SysUserFunctionRoleRelation> list = new ArrayList<>(fnRoleIds.length);
        for(int i = 0 ;i < fnRoleIds.length ;++i){
            if(fnRoleIds[i] == 0){
                continue;
            }
            SysUserFunctionRoleRelation sysAdminFnroleUserRelation = new SysUserFunctionRoleRelation();
            sysAdminFnroleUserRelation.setFunctionRoleId(fnRoleIds[i]).setUserUuid(userUuid);
            sysAdminFnroleUserRelation.init();

            list.add(sysAdminFnroleUserRelation);
        }

        saveBatch(list);
    }

    @Override
    public void removeByUserUuid(String uuid) {
        this.baseMapper.deleteByUserUuid(uuid);
    }

    @Override
    public List<Integer> getFunctionRoleIdByUserUuid(String userUuid) {
        return this.baseMapper.selectFunctionRoleIdByUserUuid(userUuid);
    }
}
